package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.management.Query;
import java.util.List;

@Controller
@RequestMapping("/travel")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private IDestinationService destinationService;



    @RequestMapping("/audit")
    @ResponseBody
    public Object audit(String id, Integer state){
        travelService.audit(id, state);
        return JsonResult.success();
    }

    @RequestMapping("/getContentById")
    @ResponseBody
    public Object getContentById(String id){
        String content = travelService.get(id).getContent();
        return JsonResult.success(content);
    }


    @RequestMapping("/testlist")
    public void testlist(){
        List<Travel> list = travelService.list();
        list.forEach(System.out::println);
    }

    //带分页的查询
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") TravelQuery qo, Model model){
        Page<Travel> page = travelService.query(qo);
        model.addAttribute("page", page);
        return "travel/list";
    }

    @RequestMapping("/listDests")
    @ResponseBody
    public List<Destination> listDests(){
        //调用目的地service的list方法，查询所有目的
        List<Destination> list = destinationService.list();
        return list;
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Travel travel){
        travelService.saveOrUpdate(travel);
        return JsonResult.success();
    }




    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(String id){
        travelService.delete(id);
        return JsonResult.success();
    }



}
